//
// Source code recreated from ConfigVo .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ht.modules.system.online.config.util;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;

import com.ht.utils.SpringContextUtils;
import org.hibernate.Session;
import com.ht.modules.system.online.config.exception.DBException;
import com.ht.modules.system.online.config.service.DbTableHandleI;
import com.ht.modules.system.online.config.service.impl.DbTableMysqlHandleImpl;
import com.ht.modules.system.online.config.service.impl.DbTableOracleHandleImpl;
import com.ht.modules.system.online.config.service.impl.DbTablePostgresHandleImpl;
import com.ht.modules.system.online.config.service.impl.DbTableSQLServerHandleImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DbTableUtil {
    private static final Logger log = LoggerFactory.getLogger(DbTableUtil.class);
    public static final String DB_TYPE_MYSQL = "MYSQL";
    public static final String DB_TYPE_ORACLE = "ORACLE";
    public static final String DB_TYPE_POSTGRESQL = "POSTGRESQL";
    public static final String DB_TYPE_SQLSERVER = "SQLSERVER";
    public static String DB_TYPE = "";

    public DbTableUtil() {
    }

    public static DbTableHandleI getTableHandle() throws SQLException, DBException {
        DbTableHandleI dbTableHandle = null;
        String dbType = getDatabaseType();
        byte var3 = -1;
        switch (dbType.hashCode()) {
            case -1955532418:
                if (dbType.equals("ORACLE")) {
                    var3 = 1;
                }
                break;
            case -1620389036:
                if (dbType.equals("POSTGRESQL")) {
                    var3 = 3;
                }
                break;
            case 73844866:
                if (dbType.equals("MYSQL")) {
                    var3 = 0;
                }
                break;
            case 912124529:
                if (dbType.equals("SQLSERVER")) {
                    var3 = 2;
                }
        }

        dbTableHandle = switch (var3) {
            case 0 -> new DbTableMysqlHandleImpl();
            case 1 -> new DbTableOracleHandleImpl();
            case 2 -> new DbTableSQLServerHandleImpl();
            case 3 -> new DbTablePostgresHandleImpl();
            default -> dbTableHandle;
        };

        return (DbTableHandleI) dbTableHandle;
    }

    public static Connection getConnection() throws SQLException {
        DataSource dataSource = (DataSource) SpringContextUtils.getApplicationContext().getBean(DataSource.class);
        return dataSource.getConnection();
    }

    public static String getDatabaseType() throws SQLException, DBException {
        DataSource dataSource = (DataSource) SpringContextUtils.getApplicationContext().getBean(DataSource.class);
        return getDatabaseType(dataSource);
    }

    public static String getDatabaseType(DataSource dataSource) throws SQLException, DBException {
        if ("".equals(DB_TYPE)) {

            try (Connection connection = dataSource.getConnection()) {
                DatabaseMetaData md = connection.getMetaData();
                String dbType = md.getDatabaseProductName().toLowerCase();
                if (dbType.contains("mysql")) {
                    DB_TYPE = "MYSQL";
                } else if (dbType.contains("oracle")) {
                    DB_TYPE = "ORACLE";
                } else if (!dbType.contains("sqlserver") && !dbType.contains("sql server")) {
                    if (!dbType.contains("postgresql")) {
                        throw new DBException("数据库类型:[" + dbType + "]不识别!");
                    }
                    DB_TYPE = "POSTGRESQL";
                } else {
                    DB_TYPE = "SQLSERVER";
                }
            } catch (Exception var7) {
                log.error(var7.getMessage(), var7);
            }
        }
        return DB_TYPE;
    }

    public static String getDatabaseType(Connection connection) throws SQLException, DBException {
        if ("".equals(DB_TYPE)) {
            DatabaseMetaData md = connection.getMetaData();
            String dbType = md.getDatabaseProductName().toLowerCase();
            if (dbType.contains("mysql")) {
                DB_TYPE = "MYSQL";
            } else if (dbType.contains("oracle")) {
                DB_TYPE = "ORACLE";
            } else if (!dbType.contains("sqlserver") && !dbType.contains("sql server")) {
                if (!dbType.contains("postgresql")) {
                    throw new DBException("数据库类型:[" + dbType + "]不识别!");
                }
                DB_TYPE = "POSTGRESQL";
            } else {
                DB_TYPE = "SQLSERVER";
            }
        }

        return DB_TYPE;
    }

    public static String getDataType(Session session) throws SQLException, DBException {
        return getDatabaseType();
    }

    public static String getTableName(String tableName, String dataBaseType) {
        byte var3 = -1;
        switch (tableName.hashCode()) {
            case -1955532418:
                if (tableName.equals("ORACLE")) {
                    var3 = 0;
                }
                break;
            case -1620389036:
                if (tableName.equals("POSTGRESQL")) {
                    var3 = 1;
                }
        }
        return switch (var3) {
            case 0 -> tableName.toUpperCase();
            case 1 -> tableName.toLowerCase();
            default -> tableName;
        };
    }

    public static Boolean judgeTableIsExit(String tableName) {
        Connection conn = null;
        ResultSet rs = null;
        Boolean var7;
        try {
            String[] types = new String[]{"TABLE"};
            conn = getConnection();
            DatabaseMetaData dbMetaData = conn.getMetaData();
            String dataBaseType = dbMetaData.getDatabaseProductName().toUpperCase();
            String tableNamePattern = getTableName(tableName, dataBaseType);
            rs = dbMetaData.getTables(null, null, tableNamePattern, types);
            if (rs.next()) {
                log.info("数据库表：【{}】已存在", tableName);
                return true;
            }
            var7 = false;
        } catch (SQLException var17) {
            throw new RuntimeException();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException var16) {
                log.error(var16.getMessage(), var16);
            }

        }

        return var7;
    }

    public static Map<String, Object> getColumnMap(List<Map<String, Object>> queryForList) {
        Map<String, Object> columnMap = new HashMap<>();
        for (Map<String, Object> stringObjectMap : queryForList) {
            columnMap.put(stringObjectMap.get("column_name").toString(), stringObjectMap);
        }
        return columnMap;
    }

    public static String getDialect() throws SQLException, DBException {
        String databaseType = getDatabaseType();
        return getDialect(databaseType);
    }

    public static String getDialect(String databaseType) throws SQLException, DBException {
        String dialect = "org.hibernate.dialect.MySQL5InnoDBDialect";
        byte var3 = -1;
        switch (databaseType.hashCode()) {
            case -1955532418:
                if (databaseType.equals("ORACLE")) {
                    var3 = 2;
                }
                break;
            case -1620389036:
                if (databaseType.equals("POSTGRESQL")) {
                    var3 = 1;
                }
                break;
            case 912124529:
                if (databaseType.equals("SQLSERVER")) {
                    var3 = 0;
                }
        }
        dialect = switch (var3) {
            case 0 -> "org.hibernate.dialect.SQLServerDialect";
            case 1 -> "org.hibernate.dialect.PostgreSQLDialect";
            case 2 -> "org.hibernate.dialect.OracleDialect";
            default -> dialect;
        };
        return dialect;
    }

    public static String translatorToDbField(String fileName) {
        return fileName;
    }
}
